<template>
    <div>
        <appdialog ref="dialog"
                   :visible="false"
                   title="重置密码"
                   width="30%"
                   @confirm="confirmAdd"
                   @cancle="cancleAdd">

                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="输入密码：" prop="password">
                        <el-input v-model="ruleForm.password" type="password" v-if="isShow">
                            <i slot="suffix" class="el-input__icon iconfont icon-kejian" @click="isShow = !isShow"></i>
                        </el-input>
                        <el-input v-model="ruleForm.password"  v-if="!isShow">
                            <i slot="suffix" class="el-input__icon iconfont icon-bukejian"  @click="isShow = !isShow"></i>
                        </el-input>
                    </el-form-item>
                       
                </el-form>

        </appdialog>
    </div>
</template>


<script>
import appdialog from "@/view/components/dialog/dialog";
import { updatePassword } from "@/service/swagger/UserController";

export default {
    components:{
     appdialog
    },
    data() {
        return {
            isShow:true,
            ruleForm:{
                password:"",
                userId:'',
            },
            rules:{
                password:[
                  { required: true, message: '请输入密码', trigger: 'blur' } 
                ]
            },
            copyRow:{}
        }
    },
    methods: {
        open(row){
            console.log(row)
            this.copyRow = row
            this.ruleForm.userId = row.userId
            this.$refs.dialog.open()
        },
        confirmAdd() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    // 验证通过
                    this.submitAdd()
                } else {
                    setTimeout(()=>{
                        this.open(this.copyRow);
                    },50)
                return false;
                }
            })
        },
        submitAdd(){
           updatePassword(this.ruleForm).then(ret=>{
               if(ret&&ret.code == 0){
                   this.cancleAdd()
                   this.$message({type: 'success',message: '重置成功!'})
               }else{
                   this.cancleAdd()
                   this.$message({type: 'error',message: '重置失败!'})
               }
           })
        },
        cancleAdd() {
            this.copyRow = {}
            this.isShow = true
            this.ruleForm = {
                password:"",
                userId:'',
            }
        }
    }
}
</script>

